/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package wn.tcpip.socket.netty.handler;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import wn.tcpip.socket.netty.cache.NettyChannelMap;
import wn.tcpip.socket.util.Byte2Hex;
import wn.tcpip.socket.netty.business.factory.ResolverFactory;
import wn.tcpip.socket.netty.business.factory.ResoverI;
import wn.tcpip.socket.netty.model.GPSBackRequest;
import wn.tcpip.socket.spring.SpringUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;

@ChannelHandler.Sharable
public  class SocksServerHandler extends SimpleChannelInboundHandler<String> {
    private Logger logger = LoggerFactory.getLogger(SocksServerHandler.class);
    @Override
    public void channelActive(final ChannelHandlerContext ctx) {
    }
    @Override
    public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        logger.info("oldinfo-----------------------------------" + msg);
        if(msg.startsWith("7878")) {
            // Send the received message to all channels but the current one.
            GPSBackRequest request = new GPSBackRequest(msg);//构造请求
            System.out.println(JSON.toJSONString(request));
            request.setChannel(ctx.channel());
            ResoverI resoverI = ResolverFactory.makeResover(request.getXieyiHao());
            if (resoverI == null) {
                ctx.close();
            } else {
                String response = resoverI.getResponse(request);
                if (StringUtils.isBlank(response)) {
                    ctx.close();
                } else {
                    byte[] bytes = Byte2Hex.HexString2Bytes(response);
                    ByteBuf responseByteBuf = Unpooled.buffer(bytes.length);
                    responseByteBuf.writeBytes(bytes);
                    ctx.writeAndFlush(responseByteBuf);
                }
            }
        }else{
            logger.info("oldinfo-----------------------------------no undefine cmd");
        }

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        logger.error(cause.getMessage(),cause);
        NettyChannelMap.remove(ctx.channel());
        ctx.close();
    }

}
